package merge

import (
	"fmt"
	"game/game-design/export"
	"game/game-design/gen"
	"game/game-design/loader"
	"os"
)

type Catalog struct {
	Metas []*loader.DesignFile
}

func CatalogFun(args []string) {
	catalogArgs := NewCatalogArgs(args)

	fileList, err := os.ReadDir(catalogArgs.SourceHome)
	if err != nil {
		fmt.Println(err)
		return
	}
	var catalog Catalog

	var readerIO export.IDesignIO = catalogArgs.Reader
	allDesignMetas := make([]*loader.DesignFile, 0)
	for _, f := range fileList {
		fileFullPath := catalogArgs.SourceHome + string(os.PathSeparator) + f.Name()
		designFiles, err := readerIO.Read(fileFullPath, catalogArgs.DesignDefine)
		if err != nil {
			continue
		}
		allDesignMetas = append(allDesignMetas, designFiles...)
	}
	catalog.Metas = allDesignMetas

	lines := gen.ReadTemplateFile(catalogArgs.TemplateFile)

	gen.Params["catalog"] = &catalog

	classBlocks := gen.NewClassBlock(lines)
	for _, clazz := range classBlocks {

		os.Mkdir(clazz.FileDir, 0755)
		content := clazz.BuildResult()

		fmt.Println("导出代码=>\t", clazz.FileName)
		err := os.WriteFile(clazz.FileDir+clazz.FileName, []byte(content), 0666)
		if err != nil {
			fmt.Println(err)
		}
	}

}
